var vum = new Vue({
el: '#app-product',
data: {
	hotData:[],
    detailData:[],
    detailPic:[],
    detailSize:[],
    firstCatData:[],
    firstCatShowData:[],
    firstCatHideData:[],
    detailPrice:null,
    selectDataSize:{
    	price:null,
    	spec:null,
    },
    currentPic:'',
    layerShow:false,
    layerShowTitle:'',
    layerShowCon:'',
    newData:[],
    newDataOne:[],
},
filters: {
  priceOld: function (value) {
    if (!value) return ''
    value = value*1.3;
	var regex = /^\+?[1-9][0-9]*$/;
	if(regex.test(value)) {
	  value = parseFloat(value);
	}
	else{
	  var percentVal =  Number(parseFloat(value).toFixed(4).slice(0,-2));
	  var s = percentVal.toString();
	  var rs = s.indexOf('.');
	  if (rs < 0) {
	    rs = s.length;
	    s += '.';
	  }
	  // while (s.length <= rs + 2) {
	  //   s += '';
	  // }
	  value = s;
	}
	value = value;
    return value
  }
},
created(){	
	this.firstCat();
	this.detailInit();
	this.hotShop();
	this.newShop();
	// this.addShopCart();
	this.shopCartList();
},
mounted(){
	
},
watch:{			    	
},
methods:{
	goCart(val){
		// console.log(val);
		window.location.href="product.html?Id="+val; 
	},
	detailInit(){
		var dataSrc = {
			id: parseInt(getQueryString('Id')),
			sign: "string"
		};
		$.ajax({
			url:doMain3+'/goods/itemDetails',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataSrc),
			success:function (datas) {
				// console.log('商品详情');
				// console.log(datas);
				vum.detailData = datas.resp;
				var detailListPic = datas.resp.item_desc.split(',');
				vum.detailPic = detailListPic.slice(0,4);
				var ext2Size = JSON.parse(datas.resp.ext2);
				vum.detailSize = ext2Size.spec.resp;
				vum.detailPrice = vum.detailSize[0].price;
				vum.selectDataSize.price = vum.detailSize[0].price;
				vum.selectDataSize.spec = vum.detailSize[0].spec;
				vum.currentPic = vum.detailData.url+vum.detailPic[0];
				// console.log(ext2Size);
			},         
			error: function(res) {
				console.log('失败');					
			},
		});
		$( ".fancybox").fancybox();
	},
	selectSize(val,str){
		vum.detailPrice = val;
		vum.selectDataSize.price = val;
		vum.selectDataSize.spec = str;
	},
	selectWallPic(val){
		vum.currentPic = val;
	},
	firstCat(){
		$.ajax({
			url:doMain3+'/goods/itemCatList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			success:function (datas) {
				// console.log('一级分类');
				// console.log(datas);
				vum.firstCatData = datas.resp;
				var lenFirstCat = vum.firstCatData.length;
				vum.firstCatShowData = vum.firstCatData.slice(0,10);
				vum.firstCatHideData = vum.firstCatData.slice(10,lenFirstCat);
			},         
			error: function(res) {
				console.log('失败');					
			},
		})
	},
	hotShop(){
		var dataHost = {
			limit: 10,
			page: 1,
			sign: "string",
		};
		$.ajax({
			url:doMain3+'/goods/zitemHotList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataHost),
			success:function (datas) {
				// console.log('热卖商品');
				vum.hotData = datas.resp;
			},
			error: function(res) {
				console.log('失败');
			},
		})
	},
	newShop(){
		var dataNew = {
			limit: 8,
			page: 1,
			shopType: "1",
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew),
			success:function (datas) {
				// console.log('最新商品');
				// console.log(datas);
				vum.newData = datas.resp;
				vum.newDataOne = vum.newData.slice(0,4);
			},         
			error: function(res) {
				console.log('失败');					
			},
		})
	},
	addShopCart(){
		var userId=localStorage.__USERMALLID__||'';
		var countNum = $('.number-count').val();
		var dataAddCart = {
			item_id: vum.detailData.id,
			item_number: parseFloat(countNum),
			price: parseFloat(vum.detailData.price),
			sign: "string",
			size: 'string',
			spec: '',
			user_id:userId 
		};
		// console.log(dataAddCart);
		$.ajax({
			url:doMain3+'/goods/addZshoppingTrolley',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart),
			success:function (datas) {
				if(datas.code=="10000"){					
					vum.layerShow = true;
					var that = this;
					setTimeout(function(){
						$('.layer-show-box').show();
					},200);
					vum.layerShowTitle = '成功加入购物车！';
    				vum.layerShowCon = '您可以去购物车结算，或者继续再挑选喜欢的商品！';


    				var userId=localStorage.__USERMALLID__||'';
					var dataCode = {
						  sign: "string",
						  user_id: userId,			
					};	
					$.ajax({
						url:doMain+'/api/userAccount',
						type:"post",
						dataType:'json',
						contentType: "application/json",
						data:JSON.stringify(dataCode),
						success:function (datas) {
							localStorage.setItem('__USERPRICE_', datas.resp.preDeposit);
						},         
						error: function(res) {
							console.log('预存款失败');
						},
					})

				}				
			},         
			error: function(res) {
				console.log('失败');					
			},
		})
	},
	layerShowSure(){
		vum.layerShow = false;
	},
	layerShowHide(){
		vum.layerShow = false;
	},
	shopCartList(){
		var userId=localStorage.__USERMALLID__||'';
		// console.log(parseInt(userId));
		var dataAddCart = {
			limit: 10,
			page: 1,
			sign: "string",
			user_id: userId
		};
		$.ajax({
			url:doMain3+'/goods/zshoppingTrolleyList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart),
			success:function (datas) {
				console.log('购物车列表');
				console.log(datas);
			},         
			error: function(res) {
				console.log('失败');					
			},
		})
	},
}
})